#include "client.h"

int readCommand(char *command, int commandLen){
    memset(command, 0, commandLen);
    fgets(command,commandLen,stdin);
    //清除换行符
    command[strcspn(command,"\n")] = '\0';
    printf("command = %s\n",command);
}
int analyzeCommand(char *command, int commandLen, char *cmd, int cmdLen, char *arg, int argLen){
    //先不考虑参数有多个的情况
    //1.做好arg的初始化
    memset(arg, 0, argLen);
    memset(cmd, 0, cmdLen);
    //2.把cmd前面的空格和换行清理掉
    char *p = command;
    while(*p ==' ' || *p == '\t'){
        if(*p == ' '){
            printf("received an ' '\n");
        }
        else{
            printf("received an '\t'\n");
        }
        p++;
    }
    //3.分离出cmd
    char *q = cmd;
    while(*p != ' ' && *p != '\t' && *p != '\0'){
        *q++ = *p++;
    }
    printf("cmd = %s\n",cmd);
/*     if(*p == 0){
        return 0;
    } */
    //4.把arg前面的空格和换行清理掉
    while(*p ==' ' || *p == '\t'){
        if(*p == ' '){
            printf("received an ' '\n");
        }
        else{
            printf("received an '\t'\n");
        }
        p++;
    }
    //5.分离出arg
    q = arg;
    while (*p != 0)
    {
        *q++ = *p++;
    }
    if(strlen(arg) == 0){
        printf("arg is null character string\n");
    }
    else{
        printf("arg = %s\n",arg);
    }
}
int matchCmd(const char *cmd){
    if(strcmp(cmd,"cd") == 0){
        printf ("cd\n");
        return CD;
    }
    else if(strcmp(cmd,"ls") == 0){
        printf ("ls\n");
        return LS;   
    }
    else if(strcmp(cmd,"puts") == 0){
        printf ("puts\n");
        return PUTS;   
    }
    else if(strcmp(cmd,"gets") == 0){
        printf ("gets\n");   
        return GETS;
    }
    else if((strcmp(cmd,"rm") == 0) || (strcmp(cmd,"remove") == 0)){
        printf ("rm\n");
        return REMOVE;   
    }
    else if(strcmp(cmd,"pwd") == 0){
        printf ("pwd\n"); 
        return PWD;  
    }
    else if(strcmp(cmd,"mkdir") == 0){
        printf ("mkdir\n");   
        return MKDIR;
    }
    else{
        printf("cmd ERROR!\n");
        return -1;
    }
}
int findCmdInterface(int sockfd, const cmd_t cmd, cmdInfo_t *pcmdinfo){
    switch (cmd)
    {
    case LS:
        LS_Client(sockfd, pcmdinfo);
        break;
    case CD:
        CD_Client(sockfd, pcmdinfo);                                                                                                                                                                                                                                 
        break;
    case PUTS:
        PUTS_Client(sockfd, pcmdinfo);
        break;
    case GETS:
        GETS_Client(sockfd, pcmdinfo);
        break;
    case REMOVE:
        REMOVE_Client(sockfd, pcmdinfo);
        break;
    case PWD:
        PWD_Client(sockfd, pcmdinfo);
        break;
    case MKDIR:
        MKDIR_Client(sockfd, pcmdinfo);
        break;
    
    default:
        return -1;
        break;
    }
}
bool recvYesOrNo(int sockfd){
    int length = 0;
    char errInfo[4] = {0};
    recv(sockfd, &length, sizeof(int), MSG_WAITALL);
    printf("length = %d\n", length);
    recv(sockfd, errInfo, length, MSG_WAITALL);
    printf("recv errInfo = %s\n",errInfo);
    if (strcmp(errInfo, "0") == 0){
        return true;
    }
    else if(strcmp(errInfo, "-1") == 0){
        return false;
    }
    else{
        printf("From recvYesOrNo: unkonwn error\n");
        exit(-1);
    }
}